function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
}
//初始化页码为1
var pageIndex = 1;
//回传的页码
var returnIndex = getQueryString("pageIndex");
if(returnIndex == null){
    returnIndex = 1
}else{
    returnIndex = getQueryString("pageIndex")
}
//获取页面总数
var pageNum;
//最后一页数目
var pageLast;
//用户总数
var totalSize;
//当页面为空时，设为1
if(pageIndex == null){
    pageIndex=1;
}
//当前页面页数
var p = returnIndex;
//页面页数加一
function add() {
    //alert(p)
    //防止越界
    if(p>=pageNum) {
        p = pageNum;
    }else{
        p = parseInt(p)+1;
    }
    return p;
}
//页面页数减一
function subtract() {
    //防止越界
    if(p<2) {
        p = 1;
    }else{
        p = p-1;
    }

    return p;
}
//页数改变时传回后台的Url
var newUrl = "/Test/useradmin/getuserlist";
//首页
function topPage() {
    p = 1;
    $.ajax({
        url : newUrl,
        type : 'GET',
        data : {
            pageIndex : p,
            pageSize:5
        },
        dataType : 'json',
        success : function(data) {
            if (data.success) {
                $('.table').html("<tr>\n" +
                    "            <th>用户名</th>\n" +
                    "            <th>姓名</th>\n" +
                    "            <th>密码</th>\n" +
                    "            <th>性别</th>\n" +
                    "            <th>民族</th>\n" +
                    "            <th>联系方式</th>\n" +
                    "            <th>身份证号</th>\n" +
                    "            <th>地址</th>\n" +
                    "            <th>Edit</th>\n" +
                    "            <th>Delete</th>\n" +
                    "        </tr>");
                jQuery.each(data.userList, function(i,item){
                    var tempHtml = '<tr id=item.userId><td >'
                        + item.userName + '</td>' +
                        '<td>'+item.uName+'</td>+' +
                        '<td>'+item.userPassword+'</td>' +
                        '<td>'+item.userSex+'</td>' +
                        '<td>'+item.userNation+'</td>' +
                        '<td>'+item.phone+'</td>' +
                        '<td>'+item.userCardId+'</td>' +
                        '<td>'+item.userAddr+'</td>' +
                        '<td><div><a href="/Test/useradmin/useroperation?userId='+item.userId+'">Edit</a></div></td>' +
                        '<td><div><a data-id = "'+item.userId+'" href="javascript:void(0)" class="delete">Delete</a></div></tr>'
                    $('.table').append(tempHtml);
                });

            }
        }

    });
}
//上一页
function prePage() {
    $.ajax({
        url : newUrl,
        type : 'GET',
        data : {
            pageIndex : subtract(),
            pageSize:5
        },
        dataType : 'json',
        success : function(data) {
            if (data.success) {
                $('.table').html("<tr>\n" +
                    "            <th>用户名</th>\n" +
                    "            <th>姓名</th>\n" +
                    "            <th>密码</th>\n" +
                    "            <th>性别</th>\n" +
                    "            <th>民族</th>\n" +
                    "            <th>联系方式</th>\n" +
                    "            <th>身份证号</th>\n" +
                    "            <th>地址</th>\n" +
                    "            <th>Edit</th>\n" +
                    "            <th>Delete</th>\n" +
                    "        </tr>");
                jQuery.each(data.userList, function(i,item){
                    var tempHtml = '<tr id=item.userId><td >'
                        + item.userName + '</td>' +
                        '<td>'+item.uName+'</td>+' +
                        '<td>'+item.userPassword+'</td>' +
                        '<td>'+item.userSex+'</td>' +
                        '<td>'+item.userNation+'</td>' +
                        '<td>'+item.phone+'</td>' +
                        '<td>'+item.userCardId+'</td>' +
                        '<td>'+item.userAddr+'</td>' +
                        '<td><div><a href="/Test/useradmin/useroperation?userId='+item.userId+'&pageIndex='+p+'"">Edit</a></div></td>' +
                        '<td><div><a data-id = "'+item.userId+'" href="javascript:void(0)" class="delete">Delete</a></div></tr>'
                    $('.table').append(tempHtml);
                });

            }
        }

    });
}
//下一页
function nextPage() {

    $.ajax({
        url: newUrl,
        type: 'GET',
        data: {
            pageIndex:add(),
            pageSize: 5
        },
        dataType: 'json',
        success: function (data) {
            if (data.success) {
                $('.table').html("<tr>\n" +
                    "            <th>用户名</th>\n" +
                    "            <th>姓名</th>\n" +
                    "            <th>密码</th>\n" +
                    "            <th>性别</th>\n" +
                    "            <th>民族</th>\n" +
                    "            <th>联系方式</th>\n" +
                    "            <th>身份证号</th>\n" +
                    "            <th>地址</th>\n" +
                    "            <th>Edit</th>\n" +
                    "            <th>Delete</th>\n" +
                    "        </tr>");
                jQuery.each(data.userList, function (i, item) {
                    var tempHtml = '<tr id=item.userId><td >'
                        + item.userName + '</td>' +
                        '<td>' + item.uName + '</td>+' +
                        '<td>' + item.userPassword + '</td>' +
                        '<td>' + item.userSex + '</td>' +
                        '<td>' + item.userNation + '</td>' +
                        '<td>' + item.phone + '</td>' +
                        '<td>' + item.userCardId + '</td>' +
                        '<td>' + item.userAddr + '</td>' +
                        '<td><div><a href="/Test/useradmin/useroperation?userId=' + item.userId + '&pageIndex='+p+'"">Edit</a></div></td>' +
                        '<td><div><a data-id = "' + item.userId + '" href="javascript:void(0)" class="delete">Delete</a></div></tr>'
                    $('.table').append(tempHtml);
                });

            }
        }
    })
}


//尾页
function lastPage() {
    p=pageNum;
    $.ajax({
        url : newUrl,
        type : 'GET',
        data : {
            pageIndex : p,
            pageSize:5
        },
        dataType : 'json',
        success : function(data) {
            if (data.success) {
                $('.table').html("<tr>\n" +
                    "            <th>用户名</th>\n" +
                    "            <th>姓名</th>\n" +
                    "            <th>密码</th>\n" +
                    "            <th>性别</th>\n" +
                    "            <th>民族</th>\n" +
                    "            <th>联系方式</th>\n" +
                    "            <th>身份证号</th>\n" +
                    "            <th>地址</th>\n" +
                    "            <th>Edit</th>\n" +
                    "            <th>Delete</th>\n" +
                    "        </tr>");
                jQuery.each(data.userList, function(i,item){
                    var tempHtml = '<tr id=item.userId><td >'
                        + item.userName + '</td>' +
                        '<td>'+item.uName+'</td>+' +
                        '<td>'+item.userPassword+'</td>' +
                        '<td>'+item.userSex+'</td>' +
                        '<td>'+item.userNation+'</td>' +
                        '<td>'+item.phone+'</td>' +
                        '<td>'+item.userCardId+'</td>' +
                        '<td>'+item.userAddr+'</td>' +
                        '<td><div><a href="/Test/useradmin/useroperation?userId='+item.userId+'&pageIndex='+p+'"">Edit</a></div></td>' +
                        '<td><div><a data-id = "'+item.userId+'" href="javascript:void(0)" class="delete">Delete</a></div></tr>'
                    $('.table').append(tempHtml);
                });

            }
        }

    });
}


$(function () {

    //初始化页面条目数
    var pageSize = 5;
    var queryuserUrl = '/Test/useradmin/getuserlist?pageIndex='+ returnIndex + '&pageSize=' + pageSize;


    var deleteUrl = '/Test/useradmin/deluserbyid';

    getList();
    //获取用户列表，并在页面显示
    function getList() {
        $.getJSON(queryuserUrl,function (data) {
            if(data.success){

                var tempHtml = '';
                //所有用户数量
                totalSize = data.num;
                //所有页数
                pageNum = Math.ceil(totalSize/pageSize);
                //最后一页人数
                pageLast = totalSize - (pageNum-1) * 5;
                data.userList.map(function (item,index) {
                    tempHtml += '<tbody><tr id=item.userId><td >'
                        + item.userName + '</td>' +
                        '<td>'+item.uName+'</td>+' +
                        '<td>'+item.userPassword+'</td>' +
                        '<td>'+item.userSex+'</td>' +
                        '<td>'+item.userNation+'</td>' +
                        '<td>'+item.phone+'</td>' +
                        '<td>'+item.userCardId+'</td>' +
                        '<td>'+item.userAddr+'</td>' +
                        '<td><div><a id="'+item.userId+'" href="/Test/useradmin/useroperation?userId='+item.userId+'&pageIndex='+returnIndex+'"">Edit</a></div></td>' +
                        '<td><div><a data-id = "'+item.userId+'" href="javascript:void(0)" class="delete">Delete</a></div></tr></tbody>'

                });
                $('.table').append(tempHtml);

            }

        });
    }
    //通过类选择器设置delete点击事件
    $('.table').on('click', '.delete',
        function(e) {
        pageNum = Math.ceil(totalSize/pageSize);
            var target = e.currentTarget;
                $.ajax({
                    url : deleteUrl,
                    type : 'POST',
                    data : {
                        pageLast:totalSize - (pageNum-1) * 5,
                        page:pageNum,
                        userId : target.dataset.id
                    },
                    dataType : 'json',
                    success : function(data) {

                        if (data.success) {
                            totalSize = totalSize - 1;
                            pageLast = totalSize - (pageNum-1) * 5;
                            pageNum = Math.ceil(totalSize/pageSize);
                            $('.table').html("<tr>\n" +
                                "            <th>用户名</th>\n" +
                                "            <th>姓名</th>\n" +
                                "            <th>密码</th>\n" +
                                "            <th>性别</th>\n" +
                                "            <th>民族</th>\n" +
                                "            <th>联系方式</th>\n" +
                                "            <th>身份证号</th>\n" +
                                "            <th>地址</th>\n" +
                                "            <th>Edit</th>\n" +
                                "            <th>Delete</th>\n" +
                                "        </tr>");
                            jQuery.each(data.userList, function(i,item){
                                var tempHtml = '<tr id=item.userId><td >'
                                    + item.userName + '</td>' +
                                    '<td>'+item.uName+'</td>+' +
                                    '<td>'+item.userPassword+'</td>' +
                                    '<td>'+item.userSex+'</td>' +
                                    '<td>'+item.userNation+'</td>' +
                                    '<td>'+item.phone+'</td>' +
                                    '<td>'+item.userCardId+'</td>' +
                                    '<td>'+item.userAddr+'</td>' +
                                    '<td><div><a href="/Test/useradmin/useroperation?userId='+item.userId+'&pageIndex='+p+'">Edit</a></div></td>' +
                                    '<td><div><a data-id = "'+item.userId+'" href="javascript:void(0)" class="delete">Delete</a></div></tr>'
                                $('.table').append(tempHtml);
                            });

                        } else {
                            $.toast('删除失败！');
                        }
                    }
                });
        });


})